<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>文件存储服务测试页面</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
            background-color: #f5f5f5;
        }
        .container {
            background-color: white;
            padding: 30px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        h1 {
            color: #333;
            text-align: center;
        }
        .section {
            margin-bottom: 30px;
            padding: 20px;
            border: 1px solid #ddd;
            border-radius: 5px;
        }
        .form-group {
            margin-bottom: 15px;
        }
        label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
        }
        input[type="file"], input[type="text"] {
            width: 100%;
            padding: 8px;
            border: 1px solid #ddd;
            border-radius: 4px;
            box-sizing: border-box;
        }
        button {
            background-color: #007bff;
            color: white;
            padding: 10px 20px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 16px;
        }
        button:hover {
            background-color: #0056b3;
        }
        .result {
            margin-top: 15px;
            padding: 10px;
            border-radius: 4px;
            display: none;
        }
        .success {
            background-color: #d4edda;
            color: #155724;
            border: 1px solid #c3e6cb;
        }
        .error {
            background-color: #f8d7da;
            color: #721c24;
            border: 1px solid #f5c6cb;
        }
        .file-list {
            margin-top: 20px;
        }
        .file-item {
            padding: 10px;
            border-bottom: 1px solid #eee;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }
        .file-item:last-child {
            border-bottom: none;
        }
        .file-link {
            color: #007bff;
            text-decoration: none;
        }
        .file-link:hover {
            text-decoration: underline;
        }
        .file-actions {
            display: flex;
            gap: 10px;
        }
        .file-actions button {
            padding: 5px 10px;
            font-size: 14px;
        }
        .refresh-btn {
            background-color: #28a745;
        }
        .refresh-btn:hover {
            background-color: #218838;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>文件存储服务测试页面</h1>
        
        <!-- 文件上传部分 -->
        <div class="section">
            <h2>文件上传</h2>
            <form id="uploadForm">
                <div class="form-group">
                    <label for="fileInput">选择文件:</label>
                    <input type="file" id="fileInput" name="file" required>
                </div>
                <button type="submit">上传文件</button>
            </form>
            <div id="uploadResult" class="result"></div>
        </div>
        
        <!-- 文件下载部分 -->
        <div class="section">
            <h2>文件下载</h2>
            <form id="downloadForm">
                <div class="form-group">
                    <label for="fileName">文件名:</label>
                    <input type="text" id="fileName" name="fileName" placeholder="请输入要下载的文件名" required>
                </div>
                <button type="submit">下载文件</button>
            </form>
            <div id="downloadResult" class="result"></div>
        </div>
        
        <!-- 文件列表部分 -->
        <div class="section">
            <h2>文件列表</h2>
            <button id="listFilesBtn" class="refresh-btn">刷新文件列表</button>
            <div id="fileList" class="file-list"></div>
        </div>
    </div>

    <script>
        // 文件上传处理
        document.getElementById('uploadForm').addEventListener('submit', function(e) {
            e.preventDefault();
            
            const fileInput = document.getElementById('fileInput');
            const file = fileInput.files[0];
            
            if (!file) {
                showResult('uploadResult', '请选择一个文件', 'error');
                return;
            }
            
            const formData = new FormData();
            formData.append('file', file);
            
            fetch('/api/files/upload', {
                method: 'POST',
                body: formData
            })
            .then(response => response.text())
            .then(data => {
                showResult('uploadResult', data, 'success');
                // 清空文件选择
                fileInput.value = '';
                // 刷新文件列表
                listFiles();
            })
            .catch(error => {
                showResult('uploadResult', '上传失败: ' + error.message, 'error');
            });
        });
        
        // 文件下载处理
        document.getElementById('downloadForm').addEventListener('submit', function(e) {
            e.preventDefault();
            
            const fileName = document.getElementById('fileName').value;
            
            if (!fileName) {
                showResult('downloadResult', '请输入文件名', 'error');
                return;
            }
            
            // 直接跳转到下载链接
            window.location.href = '/api/files/download/' + encodeURIComponent(fileName);
        });
        
        // 列出文件处理
        document.getElementById('listFilesBtn').addEventListener('click', listFiles);
        
        // 页面加载时自动列出文件
        window.addEventListener('load', listFiles);
        
        // 列出文件函数
        function listFiles() {
            fetch('/api/files/list')
            .then(response => response.json())
            .then(data => {
                const fileList = document.getElementById('fileList');
                if (data && data.length > 0) {
                    let html = '';
                    data.forEach(fileName => {
                        html += `
                            <div class="file-item">
                                <a href="/api/files/download/${encodeURIComponent(fileName)}" class="file-link">${fileName}</a>
                                <div class="file-actions">
                                    <button onclick="downloadFile('${fileName}')">下载</button>
                                    <button onclick="fillDownloadForm('${fileName}')">复制文件名</button>
                                </div>
                            </div>
                        `;
                    });
                    fileList.innerHTML = html;
                } else {
                    fileList.innerHTML = '<p>暂无文件</p>';
                }
            })
            .catch(error => {
                const fileList = document.getElementById('fileList');
                fileList.innerHTML = '<p>获取文件列表失败: ' + error.message + '</p>';
            });
        }
        
        // 下载文件函数
        function downloadFile(fileName) {
            window.location.href = '/api/files/download/' + encodeURIComponent(fileName);
        }
        
        // 填充下载表单函数
        function fillDownloadForm(fileName) {
            document.getElementById('fileName').value = fileName;
            showResult('downloadResult', '文件名已复制到下载框', 'success');
        }
        
        // 显示结果函数
        function showResult(elementId, message, type) {
            const resultElement = document.getElementById(elementId);
            resultElement.textContent = message;
            resultElement.className = 'result ' + type;
            resultElement.style.display = 'block';
            
            // 3秒后自动隐藏
            setTimeout(() => {
                resultElement.style.display = 'none';
            }, 3000);
        }
    </script>
</body>
</html>